function [Body,Args] = mysolvefail(This,NPath,NanDeriv,Sing2,BK)
% mysolvefail  [Not a public function] Create error/warning message when function solve fails.
%
% Backend IRIS function.
% No help provided.

% -IRIS Toolbox.
% -Copyright (c) 2007-2015 IRIS Solutions Team.

%#ok<*AGROW>

BR = sprintf('\n\t');

%--------------------------------------------------------------------------

Body = 'Solution not available for some parameterization(s)';
Args = { };
ixReportBK = NPath==0 | isinf(NPath);

while true
    inx = NPath==-4;
    if any(inx)
        Body = [Body,BR, ...
            'Model declared nonlinear, fails to solve ', ...
            'because of problems with steady state %s.'];
        Args{end+1} = exception.Exception.alt2str(inx);
        markAsProcessed( );
        continue
    end
    
    inx = NPath==-2;
    if any(inx)
        Body = [Body,BR, ...
            'Singularity or linear dependency in some equations %s'];
        Args{end+1} = exception.Exception.alt2str(inx);
        markAsProcessed( );
        continue
    end
    
    inx = NPath==0;
    if any(inx)
        Body = [Body,BR,'No stable solution %s'];
        Args{end+1} = exception.Exception.alt2str(inx);
        markAsProcessed( );
        continue
    end
    
    inx = isinf(NPath);
    if any(inx)
        Body = [Body,BR,'Multiple stable solutions %s'];
        Args{end+1} = exception.Exception.alt2str(inx);
        markAsProcessed( );
        continue
    end
    
    inx = imag(NPath)~=0;
    if any(inx)
        Body = [Body,BR,'Complex derivatives %s'];
        Args{end+1} = exception.Exception.alt2str(inx);
        markAsProcessed( );
        continue
    end
    
    inx = isnan(NPath);
    if any(inx)
        Body = [Body,BR,'NaNs in system matrices %s'];
        Args{end+1} = exception.Exception.alt2str(inx);
        markAsProcessed( );
        continue
    end
    
    % Singularity in state space or steady state problem
    inx = NPath==-1;
    if any(inx)
        if any(Sing2(:))
            pos = find(any(Sing2,2));
            pos = pos(:).';
            for ieq = pos
                Body = [Body,BR, ...
                    'Singularity or NaN in this measurement equation %s: %s'];
                Args{end+1} = exception.Exception.alt2str(Sing2(ieq,:));
                Args{end+1} = This.eqtn{ieq};
            end
        elseif ~This.IsLinear && isnan(This,'sstate')
            Body = [Body,BR, ...
                'Model is declared nonlinear but has some NaNs ', ...
                'in its steady state %s'];
            Args{end+1} = exception.Exception.alt2str(inx);
        else
            Body = [Body,BR, ...
                'Singularity in state-space matrices %s'];
            Args{end+1} = exception.Exception.alt2str(inx);
        end
        markAsProcessed( );        
        continue
    end
    
    inx = NPath==-3;
    if any(inx)
        Args = { };
        for ii = find(inx)
            for jj = find(NanDeriv{ii})
                Body = [Body,BR, ...
                    'NaN in derivatives of this equation %s: %s'];
                Args{end+1} = exception.Exception.alt2str(ii);
                Args{end+1} = This.eqtn{jj};
            end
        end
        markAsProcessed( );
        continue
    end
    
    break
end

if any(ixReportBK)
    Body = [Body,BR, ...
        '[Bkw variables, Unit roots, Stable roots]: ', ...
        sprintf( ...
        [exception.Exception.ALT2STR_DEFAULT_LABEL,'#%g[%g %g %g] '], ...
        [find(ixReportBK);BK(:,ixReportBK)] ...
        ) ];
end

return

    function markAsProcessed( )
        NPath(inx) = 1;
    end

end